package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.ManuscriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoji on 2015/1/8.
 * 投稿信息处理
 */
@Controller
@RequestMapping("/manuscript")
public class ManuscriptController {

    @Autowired
    private ManuscriptService manuscriptService;
    /**
     * 跳转投稿页面
     * @return
     */
    @RequestMapping("index")
    public String index(Model model,int raceId){
        model.addAttribute("raceInfo",manuscriptService.getRaceInfoById(raceId));
        return "/manuscript/index";
    }

    /**
     * 投稿
     * @param request 投稿信息
     * @return 返回投稿结果
     */
    @RequestMapping(value="addManuscript", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addManuscript(HttpServletRequest request,Integer raceId,String selfDescription,String attachment){
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("manuscript_id",null);
        map.put("race_id",raceId);
        map.put("artist_id",userInfo.getArtistId());
        map.put("description",selfDescription);
        map.put("manuscript",attachment);

        try {
            manuscriptService.addManuscript(map);
        } catch (Exception e) {
            map.put("errormsg","投稿失败");
        }
        return map;
    }
}
